<?php

class Renew_Model_TenpoStaff extends Apm_Model_Base
{

    public function getStaffList($code)
    {
        $sql = "select * from M05TENPO_STAFF_COMMENT where M05_TENPO_CD=" . $code;
        $list = $this->dbGetList($sql);
        return $list;
    }

    public function getStaffComment($shopNo)
    {
        $db_params = array();
        $db_params['ShopCd'] = $shopNo;

        $sql = "";
        $sql .= "SELECT ";
        $sql .= "  M05_TENPO_CD ";
        $sql .= ", M05_TENPO_COMMENT ";
        for ($i = 1; $i <= Apm_Config_Shop::STAFF_COMMENT_NUM; $i++)
        {
            $num = sprintf("%02d", $i);
            $sql .= ", M05_STAFF_GAZOU" . $num;
            $sql .= ", M05_STAFF_NAME" . $num;
            $sql .= ", M05_STAFF_COMMENT" . $num;
        }
        $sql .= "  FROM ";
        $sql .= "    M05TENPO_STAFF_COMMENT ";
        $sql .= " WHERE ";
        $sql .= "    M05_TENPO_CD = :ShopCd ";

        return $this->dbGetData($sql, $db_params);
    }

    public function convertStaffComment($staffCommentData)
    {
        $staffComment = array();

        // 店舗コメントを表示用コンバート
        $staffComment['COMMENT'] = $this->convertHtmlTag($staffCommentData['M05_TENPO_COMMENT']);

        // 店舗スタッフを表示用コンバート
        $staffComment['STAFF'] = array();
        for ($i = 1; $i <= Apm_Config_Shop::STAFF_COMMENT_NUM; $i++)
        {
            $num = sprintf("%02d", $i);
            // 写真
            $gazou = $staffCommentData['M05_STAFF_GAZOU' . $num];
            // 名前
            $name = $this->convertHtmlTag($staffCommentData['M05_STAFF_NAME' . $num]);
            // コメント
            $comment = $this->convertHtmlTag($staffCommentData['M05_STAFF_COMMENT' . $num]);
            // データが無ければ無視
            if (empty($gazou) && empty($name) && empty($comment))
            {
                continue;
            }
            // コメントを表示用に

            // データセット
            $staffComment['STAFF'][] = array(
                'GAZOU' => $gazou,
                'NAME' => $name,
                'COMMENT' => $comment
            );
        }
        return $staffComment;
    }

    public function convertHtmlTag($strHtml)
    {
        $resHtml = $strHtml;
        $resHtml = Apm_Util::replaceHtmlEntity($resHtml);
        $resHtml = Apm_Util::replaceUnicode($resHtml);
        $resHtml = mb_ereg_replace("\r", "", mb_ereg_replace("\n", "<br />", $resHtml));
        return $resHtml;
    }

}
